<?php

Class qbank_model extends CI_Model {

    function question_list($limit = '0') {
        $institute_id = $this->session->userdata('institute_id');

        $nor = $this->config->item('number_of_rows');
        if ($this->input->post('search_type')) {
            $search_type = $this->input->post('search_type');
            $search = $this->input->post('search');

            //$query = $this->db->query("select qbank.*, question_category.category_name, difficult_level.level_name from qbank, question_category, difficult_level where qbank.cid=question_category.cid and qbank.did=difficult_level.did and $search_type like '%$search%' and qbank.institute_id = '$institute_id' order by qid desc limit $limit, $nor");
            $query = $this->db->query("select qbank.*, question_category.category_name, difficult_level.level_name from qbank, question_category, difficult_level where qbank.cid=question_category.cid and qbank.did=difficult_level.did and $search_type like '%$search%' and qbank.institute_id = '$institute_id' order by qid desc");
        } else {
            //$query = $this->db->query("select qbank.*, question_category.category_name, difficult_level.level_name from qbank, question_category, difficult_level where qbank.cid=question_category.cid and qbank.did=difficult_level.did and qbank.institute_id = '$institute_id' order by qid desc limit $limit, $nor");
            $query = $this->db->query("select qbank.*, question_category.category_name, difficult_level.level_name from qbank, question_category, difficult_level where qbank.cid=question_category.cid and qbank.did=difficult_level.did and qbank.institute_id = '$institute_id' order by qid desc");
        }
        if ($query->num_rows() >= 1) {
            return $query->result();
        } else {
            return false;
        }
    }

    function get($criteria = false) {
        $query = null;
        if ($criteria != false) {
            $query = $this->db->get_where('qbank', $criteria);
        } else {
            $query = $this->db->get('qbank');
        }
        
        if ($query->num_rows() >= 1) {
            return $query->result_array();
        } else {
            return false;
        }
    }

    function get_list() {
        $institute_id = $this->session->userdata('institute_id');

        //$nor = $this->config->item('number_of_rows');
        if ($this->input->post('search_type')) {
            $search_type = $this->input->post('search_type');
            $search = $this->input->post('search');

            //$query = $this->db->query("select qbank.*, question_category.category_name, difficult_level.level_name from qbank, question_category, difficult_level where qbank.cid=question_category.cid and qbank.did=difficult_level.did and $search_type like '%$search%' and qbank.institute_id = '$institute_id' order by qid desc limit $limit, $nor");
            $query = $this->db->query("select qbank.*, question_category.category_name, difficult_level.level_name from qbank, question_category, difficult_level where qbank.cid=question_category.cid and qbank.did=difficult_level.did and $search_type like '%$search%' and qbank.institute_id = '$institute_id' order by qid desc");
        } else {
            //$query = $this->db->query("select qbank.*, question_category.category_name, difficult_level.level_name from qbank, question_category, difficult_level where qbank.cid=question_category.cid and qbank.did=difficult_level.did and qbank.institute_id = '$institute_id' order by qid desc limit $limit, $nor");
            $query = $this->db->query("select qbank.*, question_category.category_name, difficult_level.level_name from qbank, question_category, difficult_level where qbank.cid=question_category.cid and qbank.did=difficult_level.did and qbank.institute_id = '$institute_id' order by qid desc");
        }

        if ($query->num_rows() >= 1) {
            $array = $query->result_array();
            foreach ($array as &$value) {
                $qid = $value['qid'];
                $query2 = $this->db->query("select * from q_options where qid = '$qid'");

                $value['options'] = false;
                if ($query2->num_rows() >= 1) {
                    $value['options'] = $query2->result_array();
                }
            }
            return $array;
        } else {
            return false;
        }
    }

    function add_question() {
        $institute_id = $this->session->userdata('institute_id');

        $insert_data = array(
            'cid' => $this->input->post('cid'),
            'did' => $this->input->post('did'),
            'question' => $this->input->post('question'),
            'description' => $this->input->post('description'),
            'institute_id' => $institute_id
        );


        if ($this->db->insert('qbank', $insert_data)) {
            $qid = $this->db->insert_id();
            foreach ($_POST['option'] as $key => $value) {
                if ($key == ($this->input->post('score'))) {
                    $score = "1";
                } else {
                    $score = "0";
                }
                $insert_data = array(
                    'qid' => $qid,
                    'option_value' => $value,
                    'score' => $score,
                    'institute_id' => $institute_id
                );

                $this->db->insert('q_options', $insert_data);
            }
            return "Question added successfully";
        } else {
            return "Unable to add question";
        }
    }

    function import_question($question) {
//echo "<pre>"; print_r($question);
        $institute_id = $this->session->userdata('institute_id');
        $questioncid = $this->input->post('cid');
        $questiondid = $this->input->post('did');
        foreach ($question as $key => $singlequestion) {
            if ($key != 0) {
//                echo "<pre>";
//                print_r($singlequestion);

                if ($singlequestion['1'] != '') {
                    $insert_data = array(
                        'cid' => $questioncid,
                        'did' => $questiondid,
                        'question' => $singlequestion['1'],
                        'institute_id' => $institute_id
                    );

                    if ($this->db->insert('qbank', $insert_data)) {
                        $qid = $this->db->insert_id();
                        $optionkeycounter = 3;
                        for ($i = 1; $i <= 4; $i++) { // 10 option
                            if ($singlequestion[$optionkeycounter] != "") {
                                if ($singlequestion['2'] == $i) {
                                    $correctoption = '1';
                                } else {
                                    $correctoption = 0;
                                }
                                $insert_options = array(
                                    "qid" => $qid,
                                    "option_value" => $singlequestion[$optionkeycounter],
                                    "institute_id" => $institute_id,
                                    "score" => $correctoption
                                );
                                $this->db->insert("q_options", $insert_options);
                                $optionkeycounter++;
                            }
                        }
                    }
                }
            }
        }
        /*
          $institute_id = $this->session->userdata('institute_id');
          if($question['question'] != ""){
          $insert_data = array(
          'cid' => $question['cid'],
          'did' => $question['did'],
          'question' => $question['question'],
          'institute_id' => $institute_id
          );


          if($this->db->insert('qbank',$insert_data)){
          $qid=$this->db->insert_id();
          foreach($question['option'] as $key => $value){
          $sc=$question['score']-1;
          if($key==$sc){
          $score="1";
          }else{
          $score="0";
          }
          if($value != ""){
          $insert_data = array(
          'qid' => $qid,
          'option_value' => $value,
          'score'=> $score,
          'institute_id' => $institute_id
          );
          }
          $this->db->insert('q_options',$insert_data);
          }
          }
          }
         */
    }

    function update_question($id) {
        $insert_data = array(
            'cid' => $this->input->post('cid'),
            'did' => $this->input->post('did'),
            'question' => $this->input->post('question'),
            'description' => $this->input->post('description')
        );
        $institute_id = $this->session->userdata('institute_id');
        $this->db->where('institute_id', $institute_id);
        $this->db->where('qid', $id);
        if ($this->db->update('qbank', $insert_data)) {
            foreach ($_POST['option'] as $key => $value) {
                if ($key == ($this->input->post('score'))) {
                    $score = "1";
                } else {
                    $score = "0";
                }

                $insert_data = array(
                    'qid' => $id,
                    'option_value' => $value,
                    'score' => $score
                );
                if ($_POST['oids'][$key] >= "1") {
                    $oid = $_POST['oids'][$key];
                    $institute_id = $this->session->userdata('institute_id');
                    $this->db->where('institute_id', $institute_id);
                    $this->db->where('oid', $oid);
                    $this->db->update('q_options', $insert_data);
                    if ($value == "") {
                        $this->db->where('institute_id', $institute_id);
                        $this->db->where('oid', $oid);
                        $this->db->delete('q_options');
                    }
                } else {
                    $institute_id = $this->session->userdata('institute_id');

                    $this->db->insert('q_options', $insert_data);
                }
            }
            return "Question updated successfully";
        } else {
            return "Unable to update question";
        }
    }

    function get_question($id) {
        $institute_id = $this->session->userdata('institute_id');
        $query = $this->db->query("SELECT * FROM  `qbank` JOIN question_category ON qbank.cid = question_category.cid WHERE qbank.qid='$id' and qbank.institute_id = '$institute_id'");
        $questions = $query->row_array();
        $query = $this->db->query("SELECT * FROM  q_options WHERE qid='$id' and institute_id = '$institute_id'");
        $options = $query->result_array();
        $dataarr = array($questions, $options);
        return $dataarr;
    }

    function remove_question($id) {
        $institute_id = $this->session->userdata('institute_id');
        if ($this->db->delete('qbank', array('qid' => $id, 'institute_id' => $institute_id))) {
            $this->db->delete('q_options', array('qid' => $id, 'institute_id' => $institute_id));
            return true;
        } else {
            return false;
        }
    }

}

?>
